package org.gs.selvet;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.gs.config.CustomEventSender;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import javax.servlet.ServletConfig;
import javax.servlet.annotation.WebServlet;
import javax.xml.ws.Endpoint;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * webservlet的使用，用于添加拦截器
 * 信息
 * 
 * @author gs
 */
@WebServlet(urlPatterns = "/cxf/*", loadOnStartup = 1)
@Configuration
public class SolonCxfTestServlet extends CXFNonSpringServlet {

    private static final long serialVersionUID = 1L;

    @Inject
    private CustomEventSender customEventSender1;

    @Bean
    public LoggingFeature getLoggingFeature() {
        LoggingFeature loggingFeature = new LoggingFeature();
        loggingFeature.setLimit(1024100000);
        loggingFeature.setSender(customEventSender1);
        // loggingFeature.setSender(Solon.context().getBean("customEventSender"));
        loggingFeature.setPrettyLogging(true);
        return loggingFeature;
    }

    @Override
    protected void loadBus(ServletConfig sc) {
        super.loadBus(sc);
        Bus bus = getBus();
        BusFactory.setDefaultBus(bus);
        initCxfServices();
    }

    /**
     * 添加服务注册的方法，用于对多个服务进行注册
     * 
     * @since 1.0
     */
    private void initCxfServices() {

        //改进需要多个服务时的写法
        Map<String,String>  servicesMap = new LinkedHashMap<>();
        servicesMap.put("/SayHiService","sayHiService");
        servicesMap.put("/SayHiService2","sayHiService");
        for(Map.Entry<String,String> item:servicesMap.entrySet()){
            Endpoint.publish(item.getKey(),
                    Solon.context().getBean(item.getValue()), getLoggingFeature());
        }
    }
}
